package com.xqb.modules.service.impl;

import com.qcloud.vod.VodUploadClient;
import com.qcloud.vod.model.VodUploadRequest;
import com.qcloud.vod.model.VodUploadResponse;
import com.xqb.common.constants.MyConstants;
import com.xqb.common.utils.DateUtils;
import com.xqb.modules.domain.po.MedMedia;
import com.xqb.modules.mapper.MedMediaMapper;
import com.xqb.modules.service.IMedMediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 媒资Service业务层处理
 * 
 * @author xqb
 * @date 2024-10-15
 */
@Service
@Slf4j
public class MedMediaServiceImpl implements IMedMediaService
{
    @Autowired
    private MedMediaMapper medMediaMapper;

    /**
     * 查询媒资
     * 
     * @param id 媒资主键
     * @return 媒资
     */
    @Override
    public MedMedia selectMedMediaById(Long id)
    {
        return medMediaMapper.selectMedMediaById(id);
    }

    /**
     * 查询媒资列表
     * 
     * @param medMedia 媒资
     * @return 媒资
     */
    @Override
    public List<MedMedia> selectMedMediaList(MedMedia medMedia)
    {
        return medMediaMapper.selectMedMediaList(medMedia);
    }

    /**
     * 新增媒资
     * 
     * @param medMedia 媒资
     * @return 结果
     */
    @Override
    public int insertMedMedia(MedMedia medMedia)
    {
        medMedia.setCreateTime(DateUtils.getNowDate());
        return medMediaMapper.insertMedMedia(medMedia);
    }

    /**
     * 修改媒资
     * 
     * @param medMedia 媒资
     * @return 结果
     */
    @Override
    public int updateMedMedia(MedMedia medMedia)
    {
        medMedia.setUpdateTime(DateUtils.getNowDate());
        return medMediaMapper.updateMedMedia(medMedia);
    }

    /**
     * 批量删除媒资
     * 
     * @param ids 需要删除的媒资主键
     * @return 结果
     */
    @Override
    public int deleteMedMediaByIds(Long[] ids)
    {
        return medMediaMapper.deleteMedMediaByIds(ids);
    }

    /**
     * 删除媒资信息
     * 
     * @param id 媒资主键
     * @return 结果
     */
    @Override
    public int deleteMedMediaById(Long id)
    {
        return medMediaMapper.deleteMedMediaById(id);
    }

    @Override
    public Long uploadMedia(String filePath) {
        //指定当前腾讯云账号的secretId，secretKey
        VodUploadClient client = new VodUploadClient(MyConstants.VOD_ACCESS_KEY_ID, MyConstants.VOD_ACCESS_KEY_SECRET);
        VodUploadRequest request = new VodUploadRequest();
        request.setMediaFilePath(filePath);
        request.setProcedure("LongVideoPreset");

        try {
            VodUploadResponse response = client.upload("ap-guangzhou", request);
            // 获取FileId
            String fileId = response.getFileId();
            log.info("Upload FileId = {}", fileId);
            return Long.valueOf(fileId);
        } catch (Exception e) {
            // 业务方进行异常处理
            log.error("Upload Err", e);
            return null;
        }
    }
}
